package cn.pop.hsp_system_v2.Controller;

import cn.pop.hsp_system_v2.Mapper.SchedulingMapper;
import cn.pop.hsp_system_v2.Pojo.*;
import cn.pop.hsp_system_v2.Service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
public class AppointController {
    @Autowired
    private IAppointService appointService;
    @Autowired
    private IPatientService patientService;
    @Autowired
    private SchedulingService schedulingService;

    //显示该部门医生
    @GetMapping({"/jumpdoctorlist/{id}","/jumpdoctor"})
    public String jumpdoctorlist(@PathVariable("id")int out_patient_id, HttpSession session, Model model){
        System.out.println(out_patient_id);
        if(out_patient_id!=0){
            Doctor d=new Doctor();
            d.setOut_patient_id(out_patient_id);
            Collection<Doctor> doctors=appointService.selectDoctorList(d);
            System.out.println(doctors);
            if(doctors!=null){
                session.setAttribute("outPatintmentId",out_patient_id);
                model.addAttribute("doctors",doctors);
                return "/doctor";
            }
        }
        Collection<OutPatient> outPatients= patientService.selectAllOutPatient();
        System.out.println(outPatients);
        model.addAttribute("outpatients",outPatients);
        return "/patient/department";
    }

    //显示该医生详细信息及其排班表
    @RequestMapping("/jumpdoctorpage/{id}")
    public String jumpdoctorpage(@PathVariable("id")Long doctor_id, HttpSession session, Model model){
        if(doctor_id!=0){
            HashMap<String, Object> hashMap = schedulingService.SchedulingByDoc(doctor_id);
                model.addAttribute("hashMap",hashMap);
                System.out.println(hashMap);
                return "/detail";
        }
        return "/doctor";
    }

    //显示用户本人的预约
    @RequestMapping("/jumpAppointment")
    public String jumpAppointment(HttpSession session, Model model){
        Appointment p=new Appointment();
        Patient user= (Patient) session.getAttribute("user");
        p.setUuid(user.getUuid());
        Collection<Appointment> Alist=appointService.selectAppointmentList(p);
        System.out.println(Alist);
        model.addAttribute("appointment",Alist);

        return "/appointment";
    }

    //进入个人中心（内有医保卡信息）
    @RequestMapping("/jumpMy")
    public String jumpMy(Model model,HttpSession session){
        Patient user= (Patient) session.getAttribute("user");
        if(user!=null){
            PatientCard patientCard=appointService.selectPatientCardById(user.getId());
            session.setAttribute("card", patientCard);
            return "/";
        }
        return "/";
    }

    //生成预约
    @RequestMapping("/generateAppointment")
    public String generateAppointment(@RequestParam("id")Integer id,
                                      @RequestParam("doctor_id")int doctor_id,
                                      @RequestParam("selectTime")Integer selectTime,
                                      HttpSession session,Model model){
        Appointment appointment=new Appointment();
        Patient user= (Patient) session.getAttribute("user");
        System.out.println("user:"+user);
        System.out.println(session.getAttribute("outPatintmentId:"+"outPatintmentId"));
        appointment.setOutpatientid((Integer) session.getAttribute("outPatintmentId"));
        appointment.setDoctorid(doctor_id);
        appointment.setWorkday(schedulingService.findSchedulingById(id).getWorkday());
        appointment.setTime(selectTime);
        appointment.setState(0);
        appointService.insertAppointment(appointment);

        Appointment p=new Appointment();
        p.setUuid(user.getUuid());
        Collection<Appointment> Alist=appointService.selectAppointmentList(p);
        System.out.println(Alist);
        model.addAttribute("appointment",Alist);
        return "/appointment";
    }

    //删除预约
    @RequestMapping("/deleteAppointment")
    public String deleteAppointment(Appointment appointment,Model model){
        appointService.deleteAppointmentById(Long.valueOf(appointment.getId()));
        return "/appointment";
    }

    //医保卡充值
    @RequestMapping("/Cardrecharge")
    public String Cardrecharge(int money,HttpSession session,Model model){
        PatientCard patientCard=new PatientCard();
        Patient user= (Patient) session.getAttribute("user");
        patientCard.setUuid(user.getUuid());
        patientCard.setBalance(String.valueOf(Integer.valueOf(patientCard.getBalance())+money));
        appointService.updatePatientCard(patientCard);
        return "";
    }

    //支付账单
    @RequestMapping("/checkout")
    public String checkout(int check,int appointmentid,HttpSession session,Model model){
        PatientCard patientCard=new PatientCard();
        Patient user= (Patient) session.getAttribute("user");
        patientCard.setUuid(user.getUuid());
        patientCard.setBalance(String.valueOf(Integer.valueOf(patientCard.getBalance())-check));
        appointService.updatePatientCard(patientCard);

        Appointment appointment=new Appointment();
        appointment.setState(1);
        appointService.updateAppointment(appointment);
        return "/appointment";
    }
}
